package cn.itcast.up.common.mytest

import java.io.{File, FileOutputStream}
import java.util.Properties

import cn.itcast.up.common.HDFSUtils
import org.apache.oozie.client.OozieClient

/**
  * Oozie测试类
  */
object TestMyOozie {

  //获取当前的类加载器
  private val loader: ClassLoader = getClass.getClassLoader


  /**
    * 启动Oozie作业
    */
  def start(props: Properties) = {
    //props:

    val params: Map[String, String] = UpHolder.oozie.params
    //将配置文件中的数据和外部传入的配置进行数据合并
    for ((key,value) <- params) {
      props.setProperty(key, value)
    }
    val appPath = UpHolder.hadoop.nameNode + UpHolder.model.path.modelBase + "/tags_" + 33

    //设置coordinate路径
    props.setProperty(OozieClient.COORDINATOR_APP_PATH, appPath)
    props.setProperty("appPath", appPath)

    //将oozie作业相关的配置文件上传到HDFS上.
    //保存props中的数据
    props.store(new FileOutputStream(new File(loader.getResource("oozie/job.properties").getPath)), "我是注释...")

    //job.properties为了做备份.
    HDFSUtils.getInstance().copyFromFile(loader.getResource("oozie/job.properties").getPath, appPath + "/job.properties")
    //workflow.xml
    HDFSUtils.getInstance().copyFromFile(loader.getResource("oozie/workflow.xml").getPath, appPath + "/workflow.xml")
    //coordinator.xml
    HDFSUtils.getInstance().copyFromFile(loader.getResource("oozie/coordinator.xml").getPath, appPath + "/coordinator.xml")


    //启动作业
    val client = new OozieClient(UpHolder.oozie.url)
    //启动
    val jobId: String = client.run(props)

    println(jobId)
    //杀死任务
//    client.kill(jobId)


  }


  def main(args: Array[String]): Unit = {
//    jarPath,mainClass,start,end,freq
    val jarPath = "hdfs://bd001:8020/apps/tags/models/Tag_032/lib/model29.jar"
    val properties = new Properties()
    properties.setProperty("jarPath", jarPath)
    properties.setProperty("mainClass", "cn.itcast.up29.TestTag")
    properties.setProperty("start", "2019-09-01T01:00+0800")
    properties.setProperty("end", "2030-09-01T01:00+0800")
    properties.setProperty("freq", "0/10 * * * *")
    start(properties)
  }


}
